package com.wika.basics.base

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.wika.basics.R
import com.wika.basics.base.fragment.StackManager
import com.wika.basics.dialog.LoadingDialog
import com.wika.basics.ext.saveAs
import com.wika.basics.ext.saveAsUnChecked
import com.wika.basics.utils.StatusBarUtil
import com.wika.basics.utils.ViewUtils
import com.wika.basics.widget.TipsToast
import java.lang.reflect.ParameterizedType

/**
 *@Description: Activity基类
 *@Date: 2024/7/26 14:39
 *@Author: WangWeiShuo
 */
abstract class BaseActivity<VB: ViewBinding>: AppCompatActivity() {
    lateinit var mBinding:VB
    private val loadingDialog by lazy {
        LoadingDialog.Builder(this)
    }
    private lateinit var actionBarLeftIcon: ImageButton
    private lateinit var actionBarRightIcon: ImageButton
    private lateinit var actionBarLeftText: TextView
    private lateinit var actionBarCenterText: TextView
    private lateinit var toolBarLayout: RelativeLayout
    private var fragmentManager: StackManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setFullScreen(needFullScreen())
        val type = javaClass.genericSuperclass
        val vbClass: Class<VB> = type!!.saveAs<ParameterizedType>().actualTypeArguments[0].saveAs()
        val method = vbClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
        mBinding = method.invoke(this, layoutInflater)!!.saveAsUnChecked()
        setContentView(R.layout.activity_toolbar)
        val viewContent = findViewById<FrameLayout>(R.id.viewContent)
        viewContent.addView(mBinding.root)
        initView(savedInstanceState)
        initData()
    }

    /**
     * 初始化布局
     * @param savedInstanceState Bundle?
     */
    abstract fun initView(savedInstanceState: Bundle?)

    /**
     * 初始化数据
     */
    abstract fun initData()

    /**
     * 是否需要设置全屏
     * @return Boolean
     */
    abstract fun needFullScreen(): Boolean

    /**
     * ActionBar右边图标点击
     */
    abstract fun actionBarRightClick()

    /**
     * 加载提示框
     */
    fun showLoading(msg: String?) {
        loadingDialog.showLoading(msg)
    }

    /**
     * 关闭提示框
     */
    fun dismissLoading() {
        loadingDialog.dismissLoading()
    }

    /**
     * Toast
     * @param msg Toast内容
     */
    fun showToast(msg: String) {
        TipsToast.showTips(msg)
    }

    /**
     * 成功提示
     * @param msg String
     */
    fun showSuccessToast(msg: String) {
        TipsToast.showSuccessTips(msg)
    }

    /**
     * 失败提示
     * @param msg String
     */
    fun showFailToast(msg: String) {
        TipsToast.showFailedTips(msg)
    }

    /**
     * 警告提示
     * @param msg String
     */
    fun showWarningToast(msg: String) {
        TipsToast.showWarningTips(msg)
    }

    fun initFragment(firstFragment: BaseFragment<*>,containerId: Int){
        fragmentManager = StackManager(this)
        fragmentManager?.setContainerId(containerId)
        fragmentManager?.setFragment(firstFragment)
    }

    open fun openFragment(fragment: BaseFragment<*>) {
        fragmentManager?.addFragment(fragment, null)
    }

    open fun openFragment(fragment: BaseFragment<*>, bundle: Bundle?) {
        fragmentManager?.addFragment(fragment, bundle)
    }

    open fun openFragment(fragment: BaseFragment<*>, bundle: Bundle?, stackMode: Int) {
        fragmentManager?.addFragment(fragment, bundle, stackMode)
    }

    open fun closeFragment(fragment: BaseFragment<*>?) {
        fragmentManager?.close(fragment)
    }

    /**
     * 设置全屏
     * @param full Boolean
     */
    open fun setFullScreen(full: Boolean){
        if(full){
            StatusBarUtil.setFullScreen(this)
            requestWindowFeature(Window.FEATURE_NO_TITLE)
            hideActionBar()
        }else{
            hideActionBar()
        }
    }

    private fun hideActionBar(){
        if (supportActionBar != null) {
            supportActionBar!!.hide()
        }
        if (actionBar != null) {
            actionBar!!.hide()
        }
    }

    /**设置状态栏
     * @param darkContent true:白底黑字
     * @param statusBarColor 状态栏背景色
     */
    fun setStatusBar(darkContent: Boolean,
                     statusBarColor: Int = Color.WHITE) {
//        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.statusBarColor = statusBarColor
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (darkContent) {
                window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            } else {
                window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            }
        }
    }

    /**设置状态栏
     * @param darkContent true:白底黑字
     * @param statusBarColor 状态栏背景色
     * @param translucent 沉浸式效果，页面布局延伸到状态栏里面
     */
    fun setStatusBar(darkContent: Boolean,
                     statusBarColor: Int = Color.WHITE,
                     translucent: Boolean) {
        StatusBarUtil.setStatusBar(this,darkContent, statusBarColor, translucent)
    }

    /**
     * 设置ToolBar
     * @param actionBarTitle String 标题
     * @param titleColor Int 标题颜色
     * @param actionBarBg Int ToolBar背景
     * @param rightIcon Int 右边按钮Icon
     * @param containsStatusBar 是否集成状态栏
     * @param darkContent true:黑字,false:白字
     */
    fun setActionBar(backIcon: Int, actionBarTitle: String,titleColor: Int,actionBarBg: Int,rightIcon: Int,containsStatusBar: Boolean,darkContent: Boolean,statusBarColor: Int = Color.WHITE){

        actionBarLeftIcon = findViewById(R.id.toolbar_back)
        actionBarLeftText = findViewById(R.id.toolbar_left_text)
        toolBarLayout = findViewById(R.id.toolbar_layout)
        actionBarCenterText = findViewById(R.id.toolbar_title)
        actionBarRightIcon = findViewById(R.id.toolbar_btn_right)

        if(containsStatusBar) {
            setStatusBarTransparent()
//            StatusBarUtil.setStatusBar(this,darkContent, Color.TRANSPARENT, false)
            val params = toolBarLayout.layoutParams
            val statusHeight = StatusBarUtil.getStatusBarHeight(this)
            params.height = statusHeight + ViewUtils.dp2px(this, 50f) // 设置高度
            toolBarLayout.layoutParams = params
        }else{
            StatusBarUtil.setStatusBar(this,darkContent, statusBarColor, false)
        }

        actionBarLeftIcon.setOnClickListener {
            finish()
        }

        if(backIcon != -1){
            actionBarLeftIcon.visibility = View.VISIBLE
            actionBarLeftIcon.background = getDrawable(backIcon)
        }else{
            actionBarLeftIcon.visibility = View.INVISIBLE
        }

        if(!actionBarTitle.isNullOrEmpty()){
            actionBarCenterText.text = actionBarTitle
            actionBarCenterText.setTextColor(getColor(titleColor))
        }
        toolBarLayout.visibility = View.VISIBLE
//        toolBarLayout.setBackgroundColor(actionBarBg)
        toolBarLayout.background = getDrawable(actionBarBg)
        if(rightIcon != -1){
            actionBarRightIcon.visibility = View.VISIBLE
            actionBarRightIcon.background = getDrawable(rightIcon)
        }
        actionBarRightIcon.setOnClickListener {
            actionBarRightClick()
        }
    }

    /**
     * 设置ActionBar左边文字
     * @param txt String?
     * @param txtColor Int
     * @param txtSize Int
     */
    fun setActionBarLeftText(txt: String?,txtColor: Int,txtSize:Int){
        if(actionBarLeftText != null){
            actionBarLeftIcon.visibility = View.INVISIBLE
            actionBarLeftText.visibility = View.VISIBLE
            actionBarLeftText.text = txt
            actionBarLeftText.setTextColor(getColor(txtColor))
            actionBarLeftText.textSize = txtSize.toFloat()
        }
    }

    /**
     * 设置ActionBar中间文字
     * @param txt String?
     */
    fun setActionBarCenterText(txt: String?){
        if(actionBarCenterText != null){
            actionBarLeftText.text = txt
        }
    }

    /**
     * 设置ActionBar右边图标
     * @param isShow Boolean
     * @param rightIcon Int
     */
    fun setActionBarRightIcon(isShow: Boolean,rightIcon: Int){
        if(actionBarRightIcon != null){
            if(isShow) {
                actionBarRightIcon.visibility = View.VISIBLE
            }else{
                actionBarRightIcon.visibility = View.INVISIBLE
            }
            actionBarRightIcon.background = getDrawable(rightIcon)
        }
    }

    /**
     * 设置ActionBar背景
     * @param actionBarBg Int
     */
    fun setActionBarRightIcon(actionBarBg: Int){
        if(toolBarLayout != null){
            toolBarLayout.background = getDrawable(actionBarBg)
        }
    }

    /**
     * 设置状态栏透明
     */
    fun setStatusBarTransparent(){
        StatusBarUtil.setStatusBarTransparent(this)
    }
}